package com.yc.route;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @program: scholar_vibe
 * @description:
 * @author: 作者 huchaojie
 * @create: 2023-07-15 09:42
 */
@Component
public class CheckWorkHandler {

    @Resource
    private RedisTemplate redisTemplate;

    public ServerResponse qrCodeCheck(ServerRequest request){
        //学号  时间 课程id 班级id
        Map<String, String[]> attributes = request.servletRequest().getParameterMap();
        Mono<Map<String, String[]>> body = Mono.just(attributes);

        return body.flatMap(item ->{
            String stuid = item.get("stuid")[0];
            String time = item.get("time")[0];
            String scheduleId = item.get("scheduleId")[0];
            String classId = item.get("classId")[0];

            String key  ="QRCODECHECK::"+classId;
            Object o = redisTemplate.opsForHash().get(key, stuid);
            if(Objects.isNull(o)){
                redisTemplate.opsForHash().put(key,stuid,time);
                return Mono.just(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body("webflux考勤成功!"));
            }else{
                return Mono.just(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body("webflux重复考勤!"));
            }
        }).block();
    }
}
